package com.feimt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feimt.pojo.OrderDetail;
import com.feimt.pojo.Orders;
import com.feimt.pojo.ShoppingCart;
import com.feimt.result.R;
import com.feimt.service.OrderDetailService;
import com.feimt.service.OrderService;
import com.feimt.service.ShoppingCartService;
import com.feimt.utils.BaseContent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 宋孟林
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    private OrderService orderService;
    private OrderDetailService orderDetailService;
    private ShoppingCartService shoppingCartService;

    @Autowired
    public OrderController(OrderService orderService, OrderDetailService orderDetailService, ShoppingCartService shoppingCartService) {
        this.orderService = orderService;
        this.orderDetailService = orderDetailService;
        this.shoppingCartService = shoppingCartService;
    }

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据： {}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(Integer page, Integer pageSize ){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Long userId = BaseContent.getThreadLocal();
        log.info("当前用户id为： {}", userId);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, userId);
        Page<Orders> ordersPage = orderService.page(pageInfo, wrapper);
        return R.success(ordersPage);
    }
    @GetMapping("/page")
    public R<IPage<Orders>> page(Integer page, Integer pageSize, String number, String beginTime, String endTime){
        IPage<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(number != null, Orders::getNumber, number)
                .gt(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime)
                .lt(StringUtils.isNotEmpty(endTime), Orders::getOrderTime, endTime);
        orderService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> status(@RequestBody Map<String, String> map){
        Long id = Long.parseLong(map.get("id"));
        Integer status = Integer.parseInt(map.get("status"));
        if (id == null || status == null){
            return R.error("传入信息不合法");
        }
        Orders orders = orderService.getById(id);
        orders.setStatus(status);
        orderService.updateById(orders);
        return R.success("订单信息修改成功");
    }
    @PostMapping("/again")
    public R<String> againSubmit(@RequestBody Map<String, String> map){
        String ids = map.get("id");
        long id = Long.parseLong(ids);
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,id);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);
        shoppingCartService.clean();
        Long userId = BaseContent.getThreadLocal();
        List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(item -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            shoppingCart.setImage(item.getImage());
            Long dishId = item.getDishId();
            Long setmealId = item.getSetmealId();
            if (dishId != null) {
                shoppingCart.setDishId(dishId);
            } else {
                shoppingCart.setSetmealId(setmealId);
            }
            shoppingCart.setName(item.getName());
            shoppingCart.setDishFlavor(item.getDishFlavor());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCartList);
        return R.success("操作成功");
    }
}
